package com.mall.order.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.order.OrderQueryService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.converter.OrderConverter;
import com.mall.order.dal.entitys.*;
import com.mall.order.dal.persistence.OrderItemMapper;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dal.persistence.OrderShippingMapper;
import com.mall.order.dto.*;
import com.mall.order.utils.ExceptionProcessorUtils;
import com.mall.user.constants.SysRetCodeConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * ciggar
 * create-date: 2019/7/30-上午10:04
 */
@Slf4j
@Component
@Service
public class OrderQueryServiceImpl implements OrderQueryService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderShippingMapper orderShippingMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    OrderConverter orderConverter;

    @Override
    public OrderListResponse orderList(OrderListRequest request) {
        OrderListResponse response = new OrderListResponse();
        try {
            request.requestCheck();
            PageHelper.startPage(request.getPage(),request.getSize());

            Example example = new Example(Order.class);
            example.orderBy("createTime").desc();
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("userId",request.getUserId());
            List<Order> orders = orderMapper.selectByExample(example);
            List<OrderDetailInfo> orderDetailInfos = orderConverter.orders2detail(orders);
            PageInfo pageInfo = new PageInfo(orders);
            //遍历，根据orderID 分别查询 tb_order_item 和 tb_order_shipping
            for (OrderDetailInfo orderDetailInfo : orderDetailInfos) {
                String orderId = orderDetailInfo.getOrderId();
                List<OrderItem> orderItems = orderItemMapper.queryByOrderId(orderId);
                List<OrderItemDto> orderItemDtos = orderConverter.items2dto(orderItems);
                OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(orderId);
                OrderShippingDto orderShippingDto = orderConverter.shipping2dto(orderShipping);
                orderDetailInfo.setOrderItemDto(orderItemDtos);
                orderDetailInfo.setOrderShippingDto(orderShippingDto);
            }

            long total = pageInfo.getTotal();
            response.setDetailInfoList(orderDetailInfos);
            response.setTotal(total);
            response.setCode(SysRetCodeConstants.SUCCESS.getCode());
            response.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
        }catch (Exception e){
            log.error("OrderQueryServiceImpl.orderList occur Exception" + e);
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
        }
        return response;
    }
}
